package gmmxEditor;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;

public class Preferences implements Serializable {

	private static final long	serialVersionUID	= 4382598638864713900L;

	/**
	 ******************* 
	 * SINGLETON *
	 ******************* 
	 */
	private static Preferences	instance;

	public static void setInstance(Main main) {
		Preferences.main = main;
		instance = new Preferences();
	}

	public static Preferences getInstance() {
		if (instance == null) {
			instance = new Preferences();
		}
		return instance;
	}

	/**
	 ******************** 
	 * CUSTOM VARIABLES *
	 ******************** 
	 */

	/**
	 * Name of the file containing all the parameters
	 */
	private static final String	saveFileName		= "preferences.ini";

	/**
	 * Color of the lines
	 */
	private static Color		default_linesColor	= new Color(50, 50, 50);

	/**
	 * Pixels of the squares
	 */
	private static int			default_squareSide	= 32;

	/**
	 * Color of the square showing the selected point
	 */
	private static Color		default_squareColor	= new Color(50, 50, 50, 200);

	/**
	 ******************* 
	 * CLASS VARIABLES *
	 ******************* 
	 */

	/**
	 * Reference to main for redraw
	 */
	private static Main			main;

	/**
	 * Determines if the main should redraw
	 */
	private boolean				redraw				= false;

	/**
	 * Color of the lines
	 */
	private Color				linesColor			= default_linesColor;

	/**
	 * Pixels of the squares
	 */
	private int					squareSide			= default_squareSide;

	/**
	 * Color of the square showing the selected point
	 */
	private Color				squareColor			= default_squareColor;

	/**
	 * Constructor
	 */
	private Preferences() {
		this.loadFromFile(saveFileName);
	}

	/**
	 * Generic getter
	 * 
	 * @return Color
	 */
	public Color getLinesColor() {
		return this.linesColor;
	}

	/**
	 * Generic setter
	 * 
	 * @param linesColor
	 */
	public void setLinesColor(Color linesColor) {
		this.linesColor = linesColor;
		this.redraw = true;
	}

	/**
	 * Generic getter
	 * 
	 * @return int
	 */
	public int getSquareSide() {
		return this.squareSide;
	}

	/**
	 * Generic setter
	 * 
	 * @param squareSide
	 */
	public void setSquareSide(int squareSide) {
		this.squareSide = squareSide;
		this.redraw = true;
		Preferences.main.createMatrix();
	}

	/**
	 * Generic getter
	 * 
	 * @return Color
	 */
	public Color getSquareColor() {
		return this.squareColor;
	}

	/**
	 * Generic setter
	 * 
	 * @param squareColor
	 */
	public void setSquareColor(Color squareColor) {
		this.squareColor = squareColor;
		this.redraw = true;
	}

	public void save() {

		this.saveToFile(saveFileName);

		if (this.redraw) {
			main.repaintMap();
		}

		this.redraw = false;
	}

	/**
	 * Serializes the object
	 */
	private String serialize() {
		String ret = "";

		int paramsNumber = 3;

		ret += paramsNumber + "\n";
		ret += this.squareSide + "\n";
		ret += Utils.serializeColor(this.linesColor, Utils.colorMaskRGBA) + "\n";
		ret += Utils.serializeColor(this.squareColor, Utils.colorMaskRGBA) + "\n";

		return ret;
	}

	/**
	 * Imports the object from serialized string
	 */
	private void deSerialize(String[] serialized) {
		int i = 0;
		this.squareSide = Integer.parseInt(serialized[i++]);
		this.linesColor = Utils.deSerializeColor(serialized[i++], Utils.colorMaskRGBA);
		this.squareColor = Utils.deSerializeColor(serialized[i++], Utils.colorMaskRGBA);
	}

	/**
	 * Loads the class from file
	 * 
	 * @param filename
	 *            String
	 */
	private void loadFromFile(String fileName) {
		BufferedReader br = null;

		try {
			br = new BufferedReader(new FileReader(new File(fileName)));

			String currentLine = br.readLine();

			String serializedString[] = new String[Integer.parseInt(currentLine)];

			int i = 0;

			while ((currentLine = br.readLine()) != null) {
				serializedString[i++] = currentLine;
			}

			this.deSerialize(serializedString);

			br.close();
		} catch (FileNotFoundException e) {
			//TODO FILE NOT FOUND
		} catch (IOException e) {
			//TODO IO EXCEPTION
		}
	}

	/**
	 * Saves the current settings to the specified file
	 * 
	 * @param fileName
	 */
	private void saveToFile(String fileName) {
		BufferedWriter bw = null;

		try {
			bw = new BufferedWriter(new FileWriter(new File(fileName)));

			bw.write(this.serialize());

			bw.close();
		} catch (IOException e) {
			//TODO IO EXCEPTION
		}
	}
}
